package cn.itcast.goods.order.service;

import java.sql.SQLException;

import cn.itcast.goods.order.dao.OrderDao;
import cn.itcast.goods.order.domain.Order;
import cn.itcast.goods.pager.PageBean;
import cn.mytools.jdbc.JdbcUtils;

public class OrderService {
	private OrderDao orderDao = new OrderDao();

	public PageBean<Order> findByUid(String uid, int pc) {
		PageBean<Order> pb;
		try {
			JdbcUtils.beginTransaction();
			pb = orderDao.findByUid(uid, pc);
			JdbcUtils.commitTransaction();
			return pb;
		} catch (SQLException e) {
			try {
				JdbcUtils.rollbackTransaction();
			} catch (SQLException e1) {
			}
			throw new RuntimeException(e);
		}

	}

	public void createOrder(Order order) {
		try {
			JdbcUtils.beginTransaction();
			orderDao.createOrder(order);
			JdbcUtils.commitTransaction();
		} catch (SQLException e) {
			try {
				JdbcUtils.rollbackTransaction();
			} catch (SQLException e1) {
			}
			throw new RuntimeException(e);
		}
	}

	public Order findByOid(String oid) {
		try {
			JdbcUtils.beginTransaction();
			Order order = orderDao.findByOid(oid);
			JdbcUtils.commitTransaction();
			return order;
		} catch (SQLException e) {
			try {
				JdbcUtils.rollbackTransaction();
			} catch (SQLException e1) {
			}
			throw new RuntimeException(e);
		}
	}

	public int findStatus(String oid) {
		try {
			JdbcUtils.beginTransaction();
			int status = orderDao.findStatus(oid);
			JdbcUtils.commitTransaction();
			return status;
		} catch (SQLException e) {
			try {
				JdbcUtils.rollbackTransaction();
			} catch (SQLException e1) {
			}
			throw new RuntimeException(e);
		}
	}
	
	public void updateStatus(String oid , int status) {
		try {
			JdbcUtils.beginTransaction();
			orderDao.updateStatus(oid, status);
			JdbcUtils.commitTransaction();
		} catch (SQLException e) {
			try {
				JdbcUtils.rollbackTransaction();
			} catch (SQLException e1) {
			}
			throw new RuntimeException(e);
		}
	}

	public PageBean<Order> findAll(int pc) {
		PageBean<Order> pb;
		try {
			JdbcUtils.beginTransaction();
			pb = orderDao.findAll(pc);
			JdbcUtils.commitTransaction();
			return pb;
		} catch (SQLException e) {
			try {
				JdbcUtils.rollbackTransaction();
			} catch (SQLException e1) {
			}
			throw new RuntimeException(e);
		}
	}

	public PageBean<Order> findByStatus(int status, int pc) {
		PageBean<Order> pb;
		try {
			JdbcUtils.beginTransaction();
			pb = orderDao.findByStatus(status,pc);
			JdbcUtils.commitTransaction();
			return pb;
		} catch (SQLException e) {
			try {
				JdbcUtils.rollbackTransaction();
			} catch (SQLException e1) {
			}
			throw new RuntimeException(e);
		}
	}

}
